import 'dart:io';

import 'package:get/get.dart';
import 'package:package_info/package_info.dart';
import 'package:x_common_utils/cache_encryption_decrypt_util/cache_encryption_decrypt_util.dart';
import 'package:xinxuan/common/dialogs/user_agreement_dialog.dart';
import 'package:xinxuan/config/app_config.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:xinxuan/utils/cache_key_util.dart';

class StartPageController extends GetxController {

  final CommonPageController commonPageController = Get.find();

  @override
  void onReady() async {
    await CacheEncryptionDecryptUtil.create();
    initStart();
    super.onReady();
  }
  
  /// 第一屏展示用户协议
  initStart() async {
    // CacheUtil.instance.cleanAll();
    // List<String> res = await CacheUtil.instance.getAllKey();
    // print('res--$res');
    AppConfig.token = await CacheEncryptionDecryptUtil.getStringCacheEncryption(key: CacheKeyUtil.token, defaultValue: '');
    bool _userAgreement = await CacheEncryptionDecryptUtil.getBoolCacheEncryption(key: CacheKeyUtil.showUserAgreement, defaultValue: false);
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String appDeviceType = 'Android';
    if (Platform.isIOS) {
      appDeviceType = 'IOS';
    }
    commonPageController.appVersion.value = packageInfo.version;
    commonPageController.appDeviceType.value = appDeviceType;
    // _userAgreement = false;
    if (!_userAgreement) {
      Get.dialog(
        UserAgreementDialog(),
        barrierDismissible: false
      );
    } else {
      // /// 当前是否为审核版本
      // await commonPageController.getVersionMsg();
      // if (commonPageController.isRevirew.value) {
      //   commonPageController.checkLogin();
      //   Get.offAllNamed(AppRouters.REVIEW_COMMON_PAGE);
      // } else {
      //   commonPageController.checkLogin();
      //   Get.offAllNamed(AppRouters.COMMON_PAGE);
      // }
      /// 当前是否为审核版本
      Get.offAllNamed(AppRouters.COMMON_PAGE);
      await commonPageController.checkAppVersion(showLoading: false);
      commonPageController.checkLogin();
    }
  }

}